import Icon from '@/components/Icon';
import { useAppSelector } from '@/store';
import {
  collectArticleAction,
  getArticleDetailByIdAction,
  likeArticleAction,
} from '@/store/action/article';
import { ArticleDetailType } from '@/store/reducer/article';
import { Popup, Toast } from 'antd-mobile';
import { useState } from 'react';
import { useDispatch } from 'react-redux';
import CommentInput from '../CommentInput';
import Share from '../Share';
import styles from './index.module.scss';

type CommentFooterProps = {
  type?: 'normal' | 'reply';
  data?: ArticleDetailType;
  onClickComment?: () => void;
};
export default function CommentFooter({
  type = 'normal',
  data,
  onClickComment,
}: CommentFooterProps) {
  const [visible, setVisible] = useState(false);
  const [shareVisible, setShareVisible] = useState(false);
  const dispatch = useDispatch();
  const { originComment } = useAppSelector((state) => state.comment);
  const { detail } = useAppSelector((state) => state.article);

  const handleLike = async () => {
    const isLiking = detail.attitude === 1;
    await dispatch(likeArticleAction(detail.art_id, isLiking));
    Toast.show({ content: isLiking ? '取消点赞' : '点赞成功' });
    dispatch(getArticleDetailByIdAction(detail.art_id));
  };

  const handleCollect = async () => {
    const isCollect = detail.is_collected;
    await dispatch(collectArticleAction(detail.art_id, isCollect));
    Toast.show({ content: isCollect ? '取消收藏' : '收藏成功' });
    dispatch(getArticleDetailByIdAction(detail.art_id));
  };
  return (
    <div className={styles.root}>
      <div className="comment-footer">
        <div className="input-btn" onClick={() => setVisible(true)}>
          <Icon type="iconbianji" />
          <span>去评论</span>
        </div>

        {type === 'normal' && (
          <>
            <div className="action-item" onClick={onClickComment}>
              <Icon type="iconbtn_comment" />
              <p>评论</p>
              {!!Number(data?.comm_count) && <span className="bage">{data?.comm_count}</span>}
            </div>
            <div className="action-item" onClick={handleLike}>
              <Icon type={data?.attitude === 1 ? 'iconbtn_like_sel' : 'iconbtn_like2'} />
              <p>点赞</p>
            </div>
          </>
        )}
        <div className="action-item" onClick={handleCollect}>
          <Icon type={data?.is_collected ? 'iconbtn_collect_sel' : 'iconbtn_collect'} />
          <p>收藏</p>
        </div>
        <div className="action-item" onClick={() => setShareVisible(true)}>
          <Icon type="iconbtn_share" />
          <p>分享</p>
        </div>
      </div>

      <Popup visible={visible} destroyOnClose bodyStyle={{ height: '100vh' }}>
        <CommentInput onBack={() => setVisible(false)} name={originComment.aut_name} />
      </Popup>
      <Popup visible={shareVisible}>
        <Share onClose={() => setShareVisible(false)} />
      </Popup>
    </div>
  );
}
